/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JToolBar;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class GroupChart extends Portlet {
	private TDataBox box = new TDataBox();
	private BubbleChart chart = new BubbleChart(box) {
		protected double getShapeSize(Element element, Bubble bubble,int index) {
			return super.getShapeSize(element,bubble, index) / 300d * Math.min(getDrawBound().width, getDrawBound().height);
		}
	};

	public GroupChart() {
		super.initialize(chart);
		chart.setBackgroundVisible(true);
		chart.setBackgroundGradient(false);
		chart.setBackgroundFillColor(new Color(0, 0, 60));
		chart.setYScaleMaxValue(100);
		chart.setYScaleMinValue(0);
		chart.setXScaleMaxValue(6);
		chart.setXScaleMinValue(0);
		chart.setXScaleValueGap(1);
		chart.setBubbleAlpha(0.9f);
		chart.setValueTextVisible(false);
		chart.setXAxisFillColor(new Color(0, 0, 60));
		chart.setYAxisFillColor(new Color(0, 0, 60));
		chart.setShapeLineVisible(false);

		Element element1 = new Node();
		element1.setName("Element1");
		element1.putChartColor(Color.MAGENTA.darker());
		Element element2 = new Node();
		element2.setName("Element2");
		element2.putChartColor(Color.RED.darker());
		box.addElement(element1);
		box.addElement(element2);

		initBubble(element1, 1, 20, 50);
		initBubble(element1, 2, 25, 30);

		for (int i = 0; i < 10; i++) {
			initBubble(element1, 0.5 + 2 * Math.random(), 20 + 10 * Math.random(), 20 * Math.random());
		}
		for (int i = 0; i < 10; i++) {
			initBubble(element1, 1 + 2 * Math.random(), 20 + 10 * Math.random(), 10 * Math.random());
		}
		initBubble(element1, 4, 25, 10);
		initBubble(element1, 5, 80, 8);

		initBubble(element2, 1.5, 90, 30);
		initBubble(element2, 2, 80, 20);

		for (int i = 0; i < 10; i++) {
			initBubble(element2, 1 + 2 * Math.random(), 10 + 80 * Math.random(), 20 * Math.random());
		}
		for (int i = 0; i < 10; i++) {
			initBubble(element2, 1 + 2 * Math.random(), 10 + 60 * Math.random(), 10 * Math.random());
		}
		initBubble(element2, 4, 30, 10);
		initBubble(element2, 4.5, 20, 5);
	}

	public void initBubble(Element element, double x, double y, double size) {
		Bubble bubble = new Bubble(x, y, size);
		element.addChartBubble(bubble);
	}

	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		toolbar.add(new JLabel(" Style:"));
		final JComboBox comboBox = new JComboBox();
		comboBox.setPreferredSize(new Dimension(comboBox.getPreferredSize().width, 20));
		comboBox.addItem("Circle");
		comboBox.addItem("Diamond");
		comboBox.addItem("Rectangle");
		comboBox.addItem("Triangle");
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				switch (comboBox.getSelectedIndex()) {
				case 0:
					chart.setBubbleStyle(TWaverConst.INFLEXION_STYLE_CIRCLE);
					break;
				case 1:
					chart.setBubbleStyle(TWaverConst.INFLEXION_STYLE_DIAMOND);
					break;
				case 2:
					chart.setBubbleStyle(TWaverConst.INFLEXION_STYLE_RECTANGLE);
					break;
				case 3:
					chart.setBubbleStyle(TWaverConst.INFLEXION_STYLE_TRIANGLE);
					break;
				}
			}
		});
		toolbar.add(comboBox);
		return toolbar;
	}
}